package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.NewsCatalog;
import cn.wolfcode.trip.base.mapper.NewsCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class NewsCatalogServiceImpl implements INewsCatalogService {

    @Autowired
    private NewsCatalogMapper newsCatalogMapper;


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<NewsCatalog> list = newsCatalogMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(NewsCatalog newsCatalog) {
        //判断新增中是否存在序号
        if (newsCatalog.getSequence() == null){
            //查询sequence最大值
            Long sequence = newsCatalogMapper.selectSequence();
            if (sequence == null){
                sequence = 0L;
            }
            newsCatalog.setSequence(sequence + 1);
        }
        // 判断是更新还是保存操作
        if (newsCatalog.getId() == null){
            newsCatalogMapper.insert(newsCatalog);
        } else {
            newsCatalogMapper.updateByPrimaryKey(newsCatalog);
        }
    }


    @Override
    public List<NewsCatalog> selectAll() {
        return newsCatalogMapper.selectAll();
    }
}
